<template>
  <div class="wrapper">
    <div class="left">
      <el-card>
        <div class="list-wrapper">
          <el-input
            v-model="state.deleteTip"
            placeholder="请输入集群名称"
            clearable
            @clear="queryHandler"
          >
            <template slot="append">
              <el-button round type="primary" @click="queryHandler">
                查询
              </el-button>
            </template>
          </el-input>
          <div class="row between">
            <el-button type="primary" @click="addEdit">
              新增任务
            </el-button>
            <el-button type="primary" @click="addCategory">
              新增分类
            </el-button>
          </div>
          <div class="tree-wrapper">
            <el-tree
              :data="state.treeOption"
              :props="state.defaultProps"
              @node-click="handleNodeClick"
              default-expand-all
            >
              <template #default="{ node, data }">
                <span style="margin-right: 30px;">
                  <i :class="node.data.type == 1 ? 'el-icon-folder' : 'el-icon-document' "></i>
                  <span style="margin-left: 10px;">{{ node.label }}</span>
                </span>
                <span class="btn-section">
                  <span
                    @click="() => edit(data)">
                    编辑
                  </span>
                  <span
                    @click="() => remove(data)">
                    删除
                  </span>
                </span>
              </template>
            </el-tree>
          </div>
        </div>
      </el-card>
    </div>
    <div class="divider"></div>
    <div class="right">
      <WorkDetail v-if="state.currentTargetId" ref="workDetailRef"></WorkDetail>
      <div class="welcome-wrapper" v-else>
        <div class="welcome">
          <div class="title">Data Development</div>
          <div class="title">数据开发</div>
          <div class="new">开始新建</div>
          <div class="btn-field">
            <el-button @click="addEdit" type="primary">新增任务</el-button>
            <el-button @click="addCategory" type="primary">新增分类</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import BaseQueryPageForm from './mixins/BaseQueryPageForm'
import {delbusinessHandler} from './mixins/BaseFuncs'
import { nextTick, reactive, ref, toRefs, onActivated, defineEmits } from "vue";
import { searchTree } from './api/workDevelopment'
import request from './utils/request'
import WorkDetail from './workDetail.vue';

const state = reactive({
  currentTargetId: '',
  deleteTip: '',
  defaultProps: {
    children: "children",
    label: "name",
  },
  dataForm: {
    name: '',
  },
  treeOption: [],
});

onActivated(() => {
  getTreeOptions();
});

const emit = defineEmits(["option-changed"]);

const getLabel = (node:any) => {
  console.log('getLabel', node);
};
const handleNodeClick = (node:any) => {
  if(node.type == 2 && node.id != state.currentTargetId) {
    swithNode(node)
  }
};

const workDetailRef = ref();
const swithNode = (node:any) => {
  state.currentTargetId = node.id;
  nextTick(() => {
    workDetailRef.value.init(node);
  })
};
const queryHandler = () => {
  getTreeOptions();
};
const addCategory = (data:any) => {
  emit('option-changed', 'addCategory', data);
};
const addEdit = (data:any) => {
  emit('option-changed', 'addEdit', data);
};
const edit = (data:any) => {
  if(data.type == 1) {
    addCategory(data);
  } else {
    addEdit(data);
  }
};
const getTreeOptions = () => {
  console.log(state.treeOption);
  searchTree({id:-1, name: state.dataForm.name}).then((data) => {
    if (data) {
      state.treeOption = data;
    }
  })
};
const remove = (data:any) => {
  state.deleteTip = data.type == 1 ? '请确认是否删除该作业开发分类。' : '请确认是否删除该作业开发任务。'
  delbusinessHandler({id: data.id}, state.deleteTip, customDelHandler);
};
const customDelHandler = (params:any) => {
  request({
    url: '/flink/taskDir/deleteTaskDir',
    method: 'get',
    params
  }).then(res=> {
    getTreeOptions();
    if(params.id == state.currentTargetId) {
      state.currentTargetId = null;
    }
  });
}
</script>

<style lang="less" scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
  background-color: white;
}
.row {
  margin: 10px 0;
  display: flex;
  flex-direction: row;
}
.between {
  justify-content: space-between;
}
.welcome-wrapper {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .welcome {
    width: 400px;
    height: 260px;
    border: solid 1px #3c9afb;
    border-radius: 10px;
    padding: 40px;
  }
  .title {
    font-size: 20px;
    margin-bottom: 10px;
  }
  .new {
    margin-top: 30px;
    font-size: 16px;
    margin-bottom: 10px;
  }
  .btn-field {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
}
.wrapper {
  width: 100%;
  height: 690px;
  display: flex;
  flex-direction: row;
  .left {
    width: 300px;
    height: 100%;
    .list-wrapper {
      height: 100%;
      display: flex;
      flex-direction: column;
      .tree-wrapper {
        flex: 1;
        min-height: 0;
      }
    }
  }
  .divider {
    margin: 0 20px;
    width: 1px;
    background-color: rgba(0, 0, 0, .1);
  }
  .right {
    flex: 1;
    min-width: 0;
    height: 100%;
  }
}
:deep(.el-card) {
  height: 100%;
  .el-card__body {
    height: 100%;
  }
}
:deep(.el-tree) {
  height: 100%;
  overflow-x: auto;
  overflow-y: auto;

  .el-tree-node__children{
    overflow: visible !important;
    background-color: white;
  }
}
.btn-section {
  span {
    cursor: pointer;
    color: #3c9afb;
  }
}
</style>
